

function getJsApiConfig() {
    var url = window.location.href.split('#')[0];
    
    $requestURL = Think.U('WeChatApi/getApiConfig');

    $.post($requestURL, {currentURL:url}, function(data){
		
    	if (data.status == 1) {
    		
    		var token = data.info.token;
    		
    		wx.config({
    		      debug: false,
    		      appId: data.info.appid,
    		      timestamp: data.info.timestamp,
    		      nonceStr: data.info.nonceStr,
    		      signature: data.info.signature,
    		      jsApiList: [
    		        'getNetworkType',
    		        'chooseImage',
    		        'previewImage',
    		        'uploadImage'
    		      ]
    		});

    		wx.ready(function () {

    			var images = {localId: [], serverId: []};
    			var i = 0;
    			var count = 0;
    			var previewImages = [];
    			var lastImgURL = '';
    			var domain = window.location.host;
    			
    			//document.querySelector('#chooseImage').onclick = function () {
				$('#chooseImage').live('click', function(){
    				update();
    				wx.chooseImage({
    					count:9-count,
    					success: function (res) {
    						
    						$.each(res.localIds, function(k, v){
    							 //解决IOS无法上传的坑
    				            if (v.indexOf("wxlocalresource") != -1) {
    				                v = v.replace("wxlocalresource", "wxLocalResource");
    				            }
    				            
    				            images.localId.push(v);
    						});
    						
    						upload();
    					}
    				});
    			});
    			
    			$('.preview').live('click', function(){
    				update();
					var imgURL = $(this).attr('src');
					wx.previewImage({
						current: imgURL,
	    			    urls: previewImages
					});
				});
    			
    			function update(){

    				images.serverId = [];
    				previewImages = [];
    				count = 0;
    				
    				$('.preview').each(function(index){
    					
    					var imgURL = $(this).attr('src');
    					var serverID = $(this).attr('serverID');
    					previewImages.push(imgURL);
    					images.serverId.push(serverID);
    					count = index + 1;
    					
    					if(count > 8){
							$("#chooseImage").hide();
						}
    				});
    			}

    			function upload() {
    				
    				var ajaxDownloadURL = Think.U('Orders/ajaxDownLoad');
    				
    				wx.uploadImage({
    					localId: images.localId[i] + '',
    					success: function (res) {
    						i++;
    						count++;
    						//通过 mediaID 将微信服务器中的图片下载到本地服务器，并取本地服务器图片的URL用于预览
    						$.post(ajaxDownloadURL, {media_id:res.serverId}, function(data){
    							
    							if(data.status == 1){

    								lastImgURL = data.fullpath;
    								lastImgURL = 'http://' + domain + lastImgURL.substring(1);
    								var rid = data.record_id;
    								
    								var imgDiv = '<div><span><img class="preview" src="' + lastImgURL + '">';
    								imgDiv += '<a href="#" onclick="del(this)" rid="' + rid + '" fullpath="' + lastImgURL + '" serverID="' + res.serverId + '">';
    								imgDiv += '<img src="./Public/Api/images/upload-delete-btn.png">';
    								imgDiv += '</a></span></div>';
    								
    								$("#imageList").prepend(imgDiv);
    								
    								$("#record_id").val($("#record_id").val() + rid + ',');
    							}
    						});
    						
    						if (i < images.localId.length) {
    							upload();
    						}else{
    							if(count > 8){
    								$("#chooseImage").hide();
    							}
    							$.toast('已成功上传' + count + '张图片');
    						}
    					},
    					fail: function (res) {
    						alert(JSON.stringify(res));
    					}
    				});
    			}
    		})
		}else{
			
			alert(data.msg);
		}
	});
}
